//
//  ArticleCell.m
//  iCarStyle
//
//  Created by liunian on 13-9-9.
//  Copyright (c) 2013年 liunian. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "ArticleCell.h"
#import "Article.h"


@implementation ArticleCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)prepareForReuse{
    [super prepareForReuse];
    [_icon setImage:nil];
    [_nameLabel setText:nil];
    [_desc setText:nil];
}

- (void)updateWithArticle:(Article *)article{
    if (article.icon) {
        [self.icon setImageWithURL:[NSURL URLWithString:article.icon] placeholderImage:IMGNAMED(@"emptyLarge.png")];
        [self.nameLabel setText:article.title];
        [self.desc setText:article.desc];
        
        [self.icon setFrame:CGRectMake(6, 10, 90, 60)];
        [self.nameLabel setFrame:CGRectMake(CGRectGetMaxX(self.icon.frame) + 10, CGRectGetMinY(self.icon.frame),
                                            CGRectGetWidth(self.bounds) - CGRectGetWidth(self.icon.frame)-12, 40)];
        
        [self.desc setFrame:CGRectMake(CGRectGetMinX(self.nameLabel.frame),
                                       CGRectGetMaxY(self.nameLabel.frame),
                                       CGRectGetWidth(self.bounds) - CGRectGetWidth(self.icon.frame) -12,
                                       20)];
        
    }else{
        [self.nameLabel setFrame:CGRectMake(10, 10, CGRectGetWidth(self.bounds) - 20, 20)];
        [self.desc setFrame:CGRectMake(10, 30, CGRectGetWidth(self.bounds) - 20, 40)];
        [self.nameLabel setText:article.title];
        [self.desc setText:article.desc];
    }
}
#pragma mark - getter
- (UIImageView *)icon{
    if (!_icon) {
        _icon  = [[UIImageView alloc] initWithFrame:CGRectMake(6, 35, 90, 60)];
        _icon.backgroundColor = [UIColor clearColor];
        _icon.contentMode = UIViewContentModeScaleAspectFill;
        _icon.userInteractionEnabled = YES;
        _icon.clipsToBounds = YES;
        [self addSubview:_icon];
    }
    return _icon;
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(6, 10,CGRectGetWidth(self.bounds) - 12, 20)];
        _nameLabel.backgroundColor = [UIColor clearColor];
        _nameLabel.textColor = [UIColor darkTextColor];
        _nameLabel.font = FONT_CONTENT(16.0f);
        [_nameLabel setNumberOfLines:0];
        [self addSubview:_nameLabel];
    }
    return _nameLabel;
}

- (UILabel *)desc{
    if (!_desc) {
        _desc = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.nameLabel.frame), CGRectGetMaxY(self.nameLabel.frame),
                                                          CGRectGetWidth(self.nameLabel.frame), 40)];
        _desc.backgroundColor = [UIColor clearColor];
        _desc.textColor = [UIColor darkGrayColor];
        _desc.font = FONT_CONTENT(14.0f);
        [_desc setNumberOfLines:0];
        [self addSubview:_desc];
    }
    return _desc;
}

@end

@implementation MoreLoadCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}
- (void)prepareForReuse{
    [super prepareForReuse];
    [_indicatorView stopAnimating];
    [_nameLabel setText:nil];
}

- (UIActivityIndicatorView *)indicatorView{
    if (!_indicatorView) {
        _indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        _indicatorView.hidesWhenStopped = YES;
        [_indicatorView setFrame:CGRectMake(80, 0, 40, 40)];
        [self addSubview:_indicatorView];
    }
    return _indicatorView;
}


- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.indicatorView.frame) + 10,
                                                               CGRectGetMinY(self.indicatorView.frame),
                                                               CGRectGetWidth(self.bounds) - 100, 40)];
        _nameLabel.backgroundColor = [UIColor clearColor];
        _nameLabel.textColor = [UIColor darkTextColor];
        _nameLabel.font = FONT_CONTENT(16.0f);
        [_nameLabel setNumberOfLines:1];
        [self addSubview:_nameLabel];
    }
    return _nameLabel;
}


@end